//
//  LoginVC1.m
//  ShengHuoJia
//
//  Created by xiebin on 2017/12/18.
//  Copyright © 2017年 mingthink. All rights reserved.
//

#import "LoginVC.h"
#import "RegisterVC.h"
#import "ForgetPwdVC.h"
#import "LoginGoogleVC.h"
@interface LoginVC ()
{
    UITextField *txtf_userName;
    UITextField *txtf_pwd;
    BOOL isShowPwd;
}
@end

@implementation LoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubViews];
}
-(void)initSubViews{
    self.view.backgroundColor=GET_HEX_COLOR(@"2b3d61");
    self.navView.backgroundColor=[UIColor clearColor];
    
    UIImageView *logoImagV=[[UIImageView alloc]init];
    [self.view addSubview:logoImagV];
    logoImagV.contentMode=UIViewContentModeScaleAspectFit;
    logoImagV.image=[UIImage imageNamed:@"login_logo"];
    CGFloat whScale=1024.0/242.0;
logoImagV.sd_layout.topSpaceToView(self.view,NAVBAR_HEIGH+20).centerXEqualToView(self.view).widthIs(whScale*40).heightIs(40);
    
    UIImageView *imgv_userName = [self createImageViewWithImageName:@"login_username"];
    imgv_userName.sd_layout.topSpaceToView(logoImagV,30).leftSpaceToView(self.view, 30).widthIs(20).heightIs(20);
    
    txtf_userName = [self createTextFieldWithPlaceHolder:@"请输入邮箱或手机号" keyboardType:UIKeyboardTypeDefault];
    txtf_userName.sd_layout.leftSpaceToView(imgv_userName,5).centerYEqualToView(imgv_userName).widthIs(200).heightIs(30);
    
    UIView *view_line1 = [self createLineView];
    view_line1.sd_layout.topSpaceToView(txtf_userName,5).leftSpaceToView(self.view,30).rightSpaceToView(self.view,30).heightIs(1);
    
    UIImageView *imgv_pwd = [self createImageViewWithImageName:@"login_pwd"];
    imgv_pwd.sd_layout.topSpaceToView(view_line1,20).leftEqualToView(imgv_userName).widthIs(20).heightIs(20);
    
    txtf_pwd= [self createTextFieldWithPlaceHolder:@"请输入密码" keyboardType:UIKeyboardTypeNumberPad];
     txtf_pwd.secureTextEntry=YES;
    txtf_pwd.sd_layout.leftSpaceToView(imgv_pwd,5).centerYEqualToView(imgv_pwd).widthIs(200).heightIs(30);
    
    UIView *view_line2 = [self createLineView];
    view_line2.sd_layout.topSpaceToView(txtf_pwd,5).leftSpaceToView(self.view,30).rightSpaceToView(self.view,30).heightIs(1);
    
    
//    txtf_pwd = UITextField.new;
//    [self.view addSubview:txtf_pwd];
//    txtf_pwd.borderStyle = UITextBorderStyleNone;
//    txtf_pwd.placeholder=@"请输入密码";
//    txtf_pwd.font = GET_SYSTEM_FONT(TEXT_FONT_30);
//    txtf_pwd.clearButtonMode=UITextFieldViewModeWhileEditing;
//    txtf_pwd.secureTextEntry=YES;
//    txtf_pwd.sd_layout.topSpaceToView(txtf_userName,10).leftSpaceToView(self.view,20).rightSpaceToView(self.view,50).heightIs(30);
    
    
    UIButton *btn_showhidePwd  = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn_showhidePwd];
    btn_showhidePwd.tag=100;
    [btn_showhidePwd setImage:[UIImage imageNamed:@"login_hidepwd"] forState:UIControlStateNormal];
    btn_showhidePwd.imageView.contentMode=UIViewContentModeScaleAspectFit;
    [btn_showhidePwd addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    btn_showhidePwd.sd_layout.leftSpaceToView(txtf_pwd,5).widthIs(30).heightIs(30).centerYEqualToView(txtf_pwd);
btn_showhidePwd.imageView.sd_layout.centerXEqualToView(btn_showhidePwd).centerYEqualToView(btn_showhidePwd).widthIs(20).heightEqualToWidth();
    
    RoundButton *btn_login = [RoundButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn_login];
    btn_login.tag=101;
    [btn_login setTitle:@"登录" forState:UIControlStateNormal];
    btn_login.backgroundColor = GET_HEX_COLOR(COLOR_TABBAR_TEXT);
//    btn_login.titleLabel.font = GET_SYSTEM_FONT(TEXT_FONT_30);
    [btn_login addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    btn_login.sd_layout.topSpaceToView(view_line2,40).leftEqualToView(view_line1).rightEqualToView(view_line1).heightIs(40);
    
    UIButton *btn_forgetPwd = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn_forgetPwd];
      btn_forgetPwd.tag=103;
    [btn_forgetPwd setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [btn_forgetPwd setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn_forgetPwd.titleLabel.font = GET_SYSTEM_FONT(TEXT_FONT_26);
     btn_forgetPwd.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
    [btn_forgetPwd addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        btn_forgetPwd.sd_layout.topSpaceToView(btn_login,20).rightEqualToView(btn_login).heightIs(30).widthIs(100);

    UIButton *btn_register = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn_register];
    btn_register.tag=102;
    [btn_register setTitle:@"注册新账号" forState:UIControlStateNormal];
    [btn_register setTitleColor:GET_HEX_COLOR(COLOR_TABBAR_TEXT) forState:UIControlStateNormal];
    btn_register.titleLabel.font = GET_SYSTEM_FONT(TEXT_FONT_30);
    //btn_register.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    [btn_register addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    btn_register.sd_layout.topSpaceToView(btn_forgetPwd,30).centerXEqualToView(self.view).heightIs(30).widthIs(150);
    
//    UIButton *btn_back= [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.view addSubview:btn_back];
//    [btn_back setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
//    btn_back.tag=104;
//    [btn_back addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
//    btn_back.sd_layout.topSpaceToView(self.view,STATUS_HEIGH+7).leftSpaceToView(self.view, 20).heightIs(30).widthEqualToHeight();
    
    
}
- (UIImageView *)createImageViewWithImageName:(NSString *)imageName  {
    UIImageView *img = [UIImageView new];
    [self.view addSubview:img];
    img.image = [UIImage imageNamed:imageName];
    img.contentMode=UIViewContentModeScaleAspectFit;
    return img;
}
- (UITextField *)createTextFieldWithPlaceHolder:(NSString *)placeHolder  keyboardType:(NSInteger)type{
    UITextField *tf = [UITextField new];
    [self.view addSubview:tf];
    NSAttributedString *attributeStr=[[NSAttributedString alloc]initWithString:placeHolder attributes:@{NSForegroundColorAttributeName:GET_RGB_COLOR(255, 255, 255, 0.4),NSFontAttributeName:GET_SYSTEM_FONT(TEXT_FONT_26)}];
    tf.attributedPlaceholder=attributeStr;
    //[tf setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    //tf.placeholder = placeHolder;
    tf.borderStyle = UITextBorderStyleNone;
    //tf.delegate = self;
    //tf.tag = tag;
    tf.font = GET_SYSTEM_FONT(TEXT_FONT_26);
    [tf setTextColor:[UIColor whiteColor]];
    tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    tf.keyboardType = type;
    tf.returnKeyType = UIReturnKeyNext;
    return tf;
}
-(UIView *)createLineView{
    UIView *view_line = UIView.new;
    [self.view addSubview:view_line];
    view_line.backgroundColor = GET_HEX_COLOR(TABLE_SEPARATOR_LIGHTGRAY);
    return view_line;
}
-(void)buttonClick:(UIButton *)btn{
    if (btn.tag==100) {//显示隐藏密码
        isShowPwd=!isShowPwd;
        if (isShowPwd) {
             txtf_pwd.secureTextEntry=NO;
               [btn setImage:[UIImage imageNamed:@"login_showpwd"] forState:UIControlStateNormal];
        }else{
            txtf_pwd.secureTextEntry=YES;
            [btn setImage:[UIImage imageNamed:@"login_hidepwd"] forState:UIControlStateNormal];
        }
    }else if (btn.tag==101){//登录
        if ([txtf_userName.text isEqualToString:@""]) {
            [self.view makeToast:@"请输入用户名或手机号"];
        }else if ([txtf_pwd.text isEqualToString:@""]){
              [self.view makeToast:@"请输入密码"];
        }else{
            LoginGoogleVC *vc =[[LoginGoogleVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            //[self requestUserLogin];
        }
    }else if (btn.tag==102){//注册
        RegisterVC *vc = RegisterVC.new;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (btn.tag==103){//忘记密码
        ForgetPwdVC *vc = ForgetPwdVC.new;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (btn.tag==104){//返回
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark-------- 网络请求
-(void)requestUserLogin{
    NSMutableDictionary *paramsDict = [[NSMutableDictionary alloc]init];
     paramsDict[@"map"] =URL_USER_LOGIN;
    paramsDict[@"account"] =txtf_userName.text;
     paramsDict[@"password"] =txtf_pwd.text;
    [HttpManager requestWithProgressTitle:@"" method:HTTPS_POST path:API_PATH paramDict:paramsDict finishBlock:^(id result, AFHTTPRequestOperationManager *manager) {
        NSInteger code=[result[@"code"] integerValue];
        if (code==200) {
            if ([StringUtils isNotEmpty:result[@"data"]]){
                NSDictionary *infoDict=result[@"data"];
                [DataUtil saveDefaultInfo:infoDict[@"uid"] Key:USER_ID];
                 [DataUtil saveDefaultInfo:infoDict[@"plum_session_api"] Key:SESSION_API];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }else{
            NSString *message=result[@"msg"];
            [self.view makeToast:message];
        }

    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
